package com.jason.base;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * author sj
 */

public class BaseController<T extends BaseModel> extends BaseProcess {

    @Autowired
    BaseService<T> service;

    @ApiOperation(value = "查询列表", notes = "带分页",  produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "selectPage")
    public Object selectPage(@RequestBody(required = false) T entity){
        return setSuccessResult(service.selectPage(entity));
    }

    @ApiOperation(value = "查询列表", notes = "不带分页",  produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "selectList")
//    @PreAuthorize("hasRole('管理员')")
    public Object selectList(@RequestBody(required = false) T entity){
        return setSuccessResult(service.selectList(entity));
    }

    @ApiOperation(value = "单条查询", notes = "查询某条记录",  produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping(value = "{id}")
    public Object selectById(@PathVariable Long id){
        return setSuccessResult(service.selectById(id));
    }

    @ApiOperation(value = "新增或修改", notes = "支持新增或修改多条记录",  produces = MediaType.APPLICATION_JSON_VALUE)
    @PutMapping
    public Object insertOrUpdateBatch(@RequestBody List<T> list, HttpServletRequest request){
        request.getHeader("Authorization");
        return setSuccessResult(service.insertOrUpdateBatch(list));
    }

    @ApiOperation(value = "单条邏輯删除", notes = "邏輯删除某条记录",  produces = MediaType.APPLICATION_JSON_VALUE)
    @DeleteMapping(value = "{id}")
    public Object deleteBYId(@PathVariable Long id){
        return setSuccessResult(service.deleteById(id));
    }

    @ApiOperation(value = "批量邏輯删除", notes = "批量邏輯删除记录",  produces = MediaType.APPLICATION_JSON_VALUE)
    @DeleteMapping
    public Object deleteBatchIds(@RequestBody List<Long> ids){
        return setSuccessResult(service.deleteBatchIds(ids));
    }



    /** 异常处理 *//*
    @ExceptionHandler(Exception.class)
    public void exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception ex)
            throws Exception {
        ModelMap modelMap = new ModelMap();
        if (ex instanceof BaseException) {
            ((BaseException)ex).handler(modelMap);
        } else if (ex instanceof IllegalArgumentException) {
            new IllegalParameterException(ex.getMessage()).handler(modelMap);
        } else if (ex instanceof UnauthorizedException) {
            modelMap.put("code", HttpCode.FORBIDDEN.value().toString());
            modelMap.put("msg", StringUtils.defaultIfBlank(ex.getMessage(), HttpCode.FORBIDDEN.msg()));
        } else {
            modelMap.put("code", HttpCode.INTERNAL_SERVER_ERROR.value().toString());
            String msg = StringUtils.defaultIfBlank(ex.getMessage(), HttpCode.INTERNAL_SERVER_ERROR.msg());
            modelMap.put("msg", msg.length() > 100 ? "系统走神了,请稍候再试." : msg);
        }
        response.setContentType("application/json;charset=UTF-8");
        modelMap.put("timestamp", System.currentTimeMillis());
        logger.info("RESPONSE : " + JSON.toJSON(modelMap));
        byte[] bytes = JSON.toJSONBytes(modelMap, SerializerFeature.DisableCircularReferenceDetect);
        response.getOutputStream().write(bytes);
    }*/

}
